﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestaurantManagement.Controls;
using DevExpress.XtraEditors;
using WindowsFormsView;
using WindowsFormsPresenter.CountryRegion;
using WindowsFormsModel.BusinessObjects;
using DevExpress.XtraGrid.Views.Grid;

namespace RestaurantManagement.Modules.Category
{
    public partial class uCountryRegion : BaseModule, ICountryRegionView, ICountryRegionsView
    {
        #region Presenter

        private CountryRegionPresenter _countryRegionPresenter;
        private CountryRegionsPresenter _countryRegionsPresenter;

        #endregion

        #region Field

        public IList<CountryRegionModel> CountryRegions
        {
            set
            {
                var countryRegions = value;
                AddUserGroupToGrid(countryRegions);
            }
        }

        public string CountryRegionCode
        {
            get { return txtCountryRegionCode.Text; }
            set { txtCountryRegionCode.Text = value; }
        }

        public string CountryRegionName
        {
            get { return txtCountryRegionName.Text; }
            set { txtCountryRegionName.Text = value; }
        }

        public DateTime CreateDate
        {
            get
            {
                if (dtCreateDate.EditValue != null) { return dtCreateDate.DateTime; }
                else { return dtCreateDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtCreateDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtCreateDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public DateTime ModifiedDate
        {
            get
            {
                if (dtModifiedDate.EditValue != null) { return dtModifiedDate.DateTime; }
                else { return dtModifiedDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtModifiedDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtModifiedDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.EditValue = value; }
        }

        #endregion

        #region Style Button

        private void btnAddNew_MouseHover(object sender, EventArgs e)
        {
            btnAddNew.BackgroundImage = null;
            btnAddNew.BackgroundImageLayout = ImageLayout.Center;
            btnAddNew.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.them_icon_2);
        }

        private void btnAddNew_MouseLeave(object sender, EventArgs e)
        {
            btnAddNew.BackgroundImage = null;
            btnAddNew.BackgroundImageLayout = ImageLayout.Center;
            btnAddNew.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.them_con);
        }

        private void btnUpdate_MouseHover(object sender, EventArgs e)
        {
            btnUpdate.BackgroundImage = null;
            btnUpdate.BackgroundImageLayout = ImageLayout.Center;
            btnUpdate.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.sua_icon_2);
        }

        private void btnUpdate_MouseLeave(object sender, EventArgs e)
        {
            btnUpdate.BackgroundImage = null;
            btnUpdate.BackgroundImageLayout = ImageLayout.Center;
            btnUpdate.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.sua_icon);
        }

        private void btnDelete_MouseHover(object sender, EventArgs e)
        {
            btnDelete.BackgroundImage = null;
            btnDelete.BackgroundImageLayout = ImageLayout.Center;
            btnDelete.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.xoa_icon_2);
        }

        private void btnDelete_MouseLeave(object sender, EventArgs e)
        {
            btnDelete.BackgroundImage = null;
            btnDelete.BackgroundImageLayout = ImageLayout.Center;
            btnDelete.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.xoa_icon);
        }

        private void btnRefresh_MouseLeave(object sender, EventArgs e)
        {
            btnRefresh.BackgroundImage = null;
            btnRefresh.BackgroundImageLayout = ImageLayout.Center;
            btnRefresh.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.lammoi_icon);
        }

        private void btnRefresh_MouseHover(object sender, EventArgs e)
        {
            btnRefresh.BackgroundImage = null;
            btnRefresh.BackgroundImageLayout = ImageLayout.Center;
            btnRefresh.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.lammoi_icon_2);
        }

        #endregion

        private void AddUserGroupToGrid(IList<CountryRegionModel> countryRegions)
        {
            if ((countryRegions != null) && (countryRegions.Count > 0))
            {
                grdMain.DataSource = countryRegions;
                grdMain.ForceInitialize();

                for (int i = 0; i < grdMainView.Columns.Count; i++) { grdMainView.Columns[i].Visible = false; }

                grdMainView.Columns["CountryRegionCode"].Visible = true;
                grdMainView.Columns["CountryRegionName"].Visible = true;
                grdMainView.Columns["Description"].Visible = true;
                grdMainView.Columns["IsActive"].Visible = true;

                grdMainView.Columns["CountryRegionCode"].Caption = "Mã quốc gia";
                grdMainView.Columns["CountryRegionName"].Caption = "Tên quốc gia";
                grdMainView.Columns["Description"].Caption = "Mô tả";
                grdMainView.Columns["IsActive"].Caption = "Được sử dụng";

                grdMainView.Columns["CountryRegionCode"].VisibleIndex = 0;
                grdMainView.Columns["CountryRegionName"].VisibleIndex = 1;
                grdMainView.Columns["Description"].VisibleIndex = 2;
                grdMainView.Columns["IsActive"].VisibleIndex = 3;

                grdMainView.FocusedRowHandle = 0;
            }
            else
            {
                grdMain.DataSource = null;
                InitializeTextbox();
            }
        }

        private void InitializeTextbox()
        {
            txtCountryRegionCode.Text = null;
            txtCountryRegionName.Text = null;
            txtDescription.Text = null; dtCreateDate.EditValue = null;
            dtModifiedDate.EditValue = null;
        }

        public uCountryRegion()
        {
            InitializeComponent();

            _countryRegionPresenter = new CountryRegionPresenter(this);
            _countryRegionsPresenter = new CountryRegionsPresenter(this);
        }

        private void uCountryRegion_Load(object sender, EventArgs e)
        {
            try
            {
                _countryRegionsPresenter.Display();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void grdMainView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                if (grdMainView.CalcHitInfo(e.Location).InRow && grdMainView.FocusedRowHandle >= 0)
                {
                    var countryRegion = (CountryRegionModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    if (countryRegion != null) { _countryRegionPresenter.Display(countryRegion.CountryRegionCode); }
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void grdMainView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (grdMainView.FocusedRowHandle >= 0)
                {
                    var countryRegion = (CountryRegionModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    if (countryRegion != null) { _countryRegionPresenter.Display(countryRegion.CountryRegionCode); }
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(CountryRegionCode)) || (string.IsNullOrEmpty(CountryRegionName)))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            for (int i = 0; i < grdMainView.RowCount; i++)
            {
                if (txtCountryRegionCode.Text == grdMainView.GetRowCellValue(i, "CountryRegionCode").ToString())
                {
                    XtraMessageBox.Show("Mã quốc gia đã tồn tại trong hệ thống !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCountryRegionCode.Focus();
                    return;
                }
            }

            try
            {
                _countryRegionPresenter.Save(true);
                _countryRegionsPresenter.Display();
                XtraMessageBox.Show("Thêm mới dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(CountryRegionCode)) || (string.IsNullOrEmpty(CountryRegionName)))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtCountryRegionCode.Text != grdMainView.GetFocusedRowCellValue("CountryRegionCode").ToString())
            {
                 XtraMessageBox.Show("Bạn không được phép sửa mã quốc gia !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 txtCountryRegionCode.Text = grdMainView.GetFocusedRowCellValue("CountryRegionCode").ToString();
                 return;
            }
            try
            {
                _countryRegionPresenter.Save(false);
                _countryRegionsPresenter.Display();
                XtraMessageBox.Show("Sửa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                _countryRegionsPresenter.Display();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdMainView.FocusedRowHandle >= 0)
                {
                    var countryRegion = (CountryRegionModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    new CountryRegionPresenter(null).Delete(countryRegion.CountryRegionCode);
                    _countryRegionsPresenter.Display();
                    XtraMessageBox.Show("Xóa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    XtraMessageBox.Show("Bạn chưa chọn dòng để xóa !", "Nhà hàng Hương Việt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}
